package com.onion.search.es;

import com.onion.search.es.exception.ESException;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Delete;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Get;
import io.searchbox.indices.Refresh;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;


public class ElasticSearchClient {

    private Logger logger = LoggerFactory.getLogger(ElasticSearchClient.class);

    private static final String ES_SERVER_ADDRESS = "es.server.address";
    private static final String ES_USERNAME = "es.username";
    private static final String ES_PASSWORD = "es.password";

    private JestClient jestClient;

    /**
     * 获取数据文档
     * @param dataId
     * @return
     */
    public <T extends EsData> T getDoc(String indexName, String dataId, Class<T> clazz){
        try {
            DocumentResult jestResult = (DocumentResult)jestClient.execute(new Get.Builder(indexName, dataId).build());
            if(jestResult.isSucceeded()){
                return jestResult.getSourceAsObject(clazz);
            }else{
                logger.error("ES获取文档数据出错:indexName={}, dataId={}", indexName, dataId, jestResult.getErrorMessage());
                throw new ESException(-1, jestResult.getErrorMessage());
            }
        } catch (IOException exp) {
            logger.error("ES获取文档数据IO异常: index={}, dataId={}", indexName, dataId, exp);
            throw new ESException(-1, "ES获取文档数据IO异常:" + exp.getMessage());
        }
    }

    /**
     * 新增数据文档
     * @param indexName
     * @param data
     * @return
     */
    public <T extends EsData> int addDoc(String indexName, T data){
        return 0;
    }

    /**
     * 更新数据文档
     * @param indexName
     * @param data
     * @return
     */
    public <T extends EsData> int updateDoc(String indexName, T data){
        return 0;
    }

    /**
     * 新增或更新数据文档
     * @param indexName
     * @param data
     * @return
     */
    public <T extends EsData> int replaceDoc(String indexName, T data){
        return 0;
    }

    /**
     * 删除ES数据文档
     * @param indexName
     * @param indexType
     * @param dataId
     * @return
     */
    public int deleteDoc(String indexName, String indexType, String dataId){
        try {
            JestResult jestResult = jestClient.execute(new Delete.Builder(dataId).index(indexName).type(indexType).build());
            if(jestResult.isSucceeded()){
                return 1;
            }else{
                logger.error("ES删除文档失败: index={}，errmsg={}", indexName, jestResult.getErrorMessage());
                throw new ESException(-1, "ES删除文档失败:" + jestResult.getErrorMessage());
            }
        } catch (IOException exp) {
            logger.error("ES删除文档IO异常: index={}", indexName, exp);
            throw new ESException(-1, "ES删除文档IO异常:" + exp.getMessage());
        }
    }

    /**
     * ES索引刷新
     * @param indexName
     */
    public void refreshIndex(String indexName){
        try {
            JestResult jestResult = jestClient.execute(new Refresh.Builder().addIndex(indexName).build());
            if(jestResult.isSucceeded()){
                return;
            }else{
                logger.error("ES刷新索引失败: index={}，errmsg={}", indexName, jestResult.getErrorMessage());
                throw new ESException(-1, "ES刷新索引失败:" + jestResult.getErrorMessage());
            }
        } catch (IOException exp) {
            logger.error("ES刷新索引IO异常: index={}", indexName, exp);
            throw new ESException(-1, "ES刷新索引IO异常:" + exp.getMessage());
        }
    }

}